package com.inspire.common.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author ：inspire
 * @date ：Created in 2022/8/12 14:10
 * @description：
 * @modified By：inspire
 * @version:
 */
public class MsgEncoder extends MessageToByteEncoder<Msg>{
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Msg msg, ByteBuf buf) throws Exception {
        ThreadLocalTest.set("bbb");
        buf.writeInt(msg.getMsgType().ordinal());
        byte[] bytes = msg.toBytes();
        buf.writeInt(bytes.length);
        buf.writeBytes(bytes);
    }
}
class ThreadLocalTest {
    private static ThreadLocal<String> familyFortunes = new ThreadLocal<String>() {
        @Override
        protected String initialValue() {

            return "aaa";
        }
    };

    public static String get() {
        return familyFortunes.get();
    }

    public static void set(String value) {
        familyFortunes.set(value);
    }

}
